package avicit.bdp.dms.dss.rest;

import avicit.bdp.common.dto.CommonVO;
import avicit.bdp.dms.dss.dto.DssUserDTO;
import avicit.bdp.dms.dss.service.DssUserService;
import avicit.platform6.api.commonselect.CommonSelectNode;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.core.restclient.RestClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户信息接口
*/
@RestController
@RequestMapping("/api/bdp/dms/dss/user")
@Api(tags = "用户信息REST接口")
public class DssUserRest {

    @Autowired
    private DssUserService dssUserService;
    @Autowired
    private RestClient restClient;

    @ApiOperation(value = "查询用户树")
    @PostMapping("/findUserTree")
    public ResponseMsg<List<CommonSelectNode>> findUserTree(@RequestBody Map queryMap){
         String url = "/api/appsys/common/select/getUserSelectList/v1";
         /*queryMap.put("currentTab","dept");*/
         queryMap.put("viewSystemUser",false);
         ResponseMsg<List<CommonSelectNode>> responseMsg = restClient.doPost(SystemConstant.SERVICE_NAME, url, queryMap,
                 new ParameterizedTypeReference<ResponseMsg<List<CommonSelectNode>>>() {
         });
         return responseMsg;
    }

    @ApiOperation(value = "分页查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "关键词", name = "keyWords", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/findUserList")
    public ResponseMsg<QueryRespBean<DssUserDTO>> findUserList(@RequestParam(required = false) String keyWords,
                                                              @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                              @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception{
        ResponseMsg<QueryRespBean<DssUserDTO>> responseMsg = new ResponseMsg<>();

        QueryRespBean<DssUserDTO> result = dssUserService.findUserList(keyWords,pageNo,pageSize);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    @ApiOperation(value = "新增用户对象")
    @PostMapping("/insertUser")
    public ResponseMsg<String> insertUser(@RequestBody CommonVO commonVO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String userIds = commonVO.getUserIds();
        String userNames = commonVO.getUserNames();
        dssUserService.insertUser(userIds,userNames);
        return responseMsg;
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户对象")
    @PostMapping("/updateUser")
    public ResponseMsg<String> updateUser(@RequestBody DssUserDTO dssUser) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        dssUserService.updateUser(dssUser);
        return responseMsg;
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "刪除用户对象")
    @PostMapping("/deleteUser")
    public ResponseMsg<Integer> deleteUser(@RequestBody CommonVO commonVO) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String id = commonVO.getId();
        dssUserService.deleteUser(id);
        return responseMsg;
    }

    @ApiOperation(value = "更新token")
    @PostMapping("/updateToken")
    public ResponseMsg<String> updateToken(@RequestBody CommonVO commonVO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String id = commonVO.getId();
        responseMsg.setResponseBody(dssUserService.updateToken(id));
        return responseMsg;
    }

    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/findUserByUserId")
    public ResponseMsg<DssUserDTO> findUserByUserId(){
        ResponseMsg<DssUserDTO> responseMsg = new ResponseMsg<>();

        DssUserDTO result = dssUserService.findUserByUserId();
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

}